<?php
namespace app\admin\controller;

class ReportController extends CommonController
{
    public $model;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = db('System')->where(['id'=>['>','0']])->find();
        $this->assign([
            'system' => $this->model
        ]);
    }
    public function user()
    {
        $start_time = input('start_time') ? strtotime(input('start_time')) : strtotime(date('Y-m-d',strtotime('-1 month')));
        $end_time = input('end_time') ? strtotime(input('end_time')) : strtotime(date('Y-m-d',time())) + 24 * 3600 -1;
        $res = [
            'time'=>[],
            'barUser'=>[],
            'barFirst'=>[],
            'barNoFirst'=>[],
        ];
        for($i = $start_time; $i <= $end_time ; $i += 24 * 3600){
            $temp = [];
            $where['add_time'] = [['gt',$i],['elt',$i+24*3600]];
            $list = db('User')->where($where)->field('type')->select();
            foreach($list as $item){
                $temp[intval($item['type'])] ++;
            }
            $res['barUser'][] = $temp[0] ?: 0;
            $res['barFirst'][] = $temp[1] ?: 0;
            $res['barNoFirst'][] = $temp[2] ?: 0;
            $res['time'][] = date('Y-m-d',$i);
        }
        $topData['user'] = db('User')->where(['type'=>0])->count() ?: 0;
        $topData['first'] = db('User')->where(['type'=>1])->count() ?: 0;
        $topData['noFirst'] = db('User')->where(['type'=>2])->count() ?: 0;
        $this->assign(['res'=>json_encode($res),'topData'=>$topData]);
        return $this->fetch('user');
    }

    public function charge(){
        $start_time = input('start_time') ? strtotime(input('start_time')) : strtotime(date('Y-m-d',strtotime('-1 month')));
        $end_time = input('end_time') ? strtotime(input('end_time')) : strtotime(date('Y-m-d',time())) + 24 * 3600 -1;
        $start_time_str = date('Y-m-d',$start_time);
        $end_time_str = date('Y-m-d',$end_time);
        $res['time'] = [];
        $res['amount'] = [];
        $topData = 0;
        //时间段内每日循环
        for($i = $start_time; $i <= $end_time ; $i += 24 * 3600){
            $where['addtime'] = [['gt',$i],['elt',$i+24*3600]];
            $where['desc'] = '充值余额';
            $money = db('UserAccountLog')->field('sum(money) as money')->where($where)->find();
            $money = $money['money']?:0;
            $topData += $money ?: 0;
            $res['time'][] = date('Y-m-d',$i);
            $res['amount'][] = $money;
        }
        $this->assign([
            'start_time_str' => $start_time_str,
            'end_time_str' => $end_time_str,
            'topData' => $topData,
            'res' => json_encode($res)
        ]);
        return $this->fetch('charge');

    }

    public function sale()
    {
        $start_time = input('start_time') ? strtotime(input('start_time')) : strtotime(date('Y-m-d',strtotime('-1 month')));
        $end_time = input('end_time') ? strtotime(input('end_time')) : strtotime(date('Y-m-d',time())) + 24 * 3600 -1;
        $start_time_str = date('Y-m-d',$start_time);
        $end_time_str = date('Y-m-d',$end_time);
        $res['time'] = [];
        $res['amount'] = [];
        $res['order'] = [];
        $topData = 0;
        //时间段内每日循环
        for($i = $start_time; $i <= $end_time ; $i += 24 * 3600){
            $order_amount = 0;
            $order_count = 0;
            $where['add_time'] = [['gt',$i],['elt',$i+24*3600]];
            $where['pay_status'] = 1;
            $where_agent = $where;
            $where_agent['consigner_id'] = 0;
            $orderList = db('Order')->field('order_amount')->where($where)->select();
            foreach($orderList as $order){
                $order_amount += $order['order_amount'];
                $order_count ++;
            }
            $topData += $order_amount ?: 0;
            $res['time'][] = date('Y-m-d',$i);
            $res['amount'][] = $order_amount;
            $res['order'][] = $order_count;
        }
        $this->assign([
            'start_time_str' => $start_time_str,
            'end_time_str' => $end_time_str,
            'topData' => $topData,
            'res' => json_encode($res)
        ]);
        return $this->fetch('sale');
    }

    public function order()
    {
        $start_time = input('start_time') ? strtotime(input('start_time')) : strtotime(date('Y-m-d',strtotime('-1 month')));
        $end_time = input('end_time') ? strtotime(input('end_time')) : strtotime(date('Y-m-d',time())) + 24 * 3600 -1;
        $start_time_str = date('Y-m-d',$start_time);
        $end_time_str = date('Y-m-d',$end_time);
        $topData = [
            'user'=>0,
            'first'=>0,
            'noFirst'=>0
        ];
        $res = [
            'time'=>[],
            'user'=>[],
            'first'=>[],
            'noFirst'=>[]
        ];
        for($i = $start_time; $i <= $end_time ; $i += 24 * 3600){
            $temp = [];
            $where['o.add_time'] = [['gt',$i],['elt',$i+24*3600]];
            $where['o.pay_status'] = 1;
            $order = db('Order')
            ->alias('o')
            ->join('__USER__ u','o.user_id=u.id','LEFT')
            ->where($where)
            ->field('u.type as user_type')
            ->select();
            foreach($order as $item){
                $temp[intval($item['user_type'])] ++;
            }
            $topData['user'] += $res['user'][] = $temp[0] ?: 0;
            $topData['first'] += $res['first'][] = $temp[1] ?: 0;
            $topData['noFirst'] += $res['noFirst'][] = $temp[2] ?: 0;
            $res['time'][] = date('Y-m-d',$i);
        }
        $this->assign([
            'start_time_str' => $start_time_str,
            'end_time_str' => $end_time_str,
            'topData' => $topData,
            'res' => json_encode($res)
        ]);
        return $this->fetch('order');
    }
}